package com.redlion.object;

import org.andengine.entity.scene.Scene;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.util.adt.pool.GenericPool;
import org.andengine.util.debug.Debug;

import android.hardware.SensorManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.redlion.Game;
import com.redlion.weapon.Cannon;

public class ShotPool extends GenericPool<Shot> {
	public ITextureRegion mTextureRegion;
	public FixtureDef mFixtureDef;
	private Scene mScene;
	private PhysicsWorld pw;

	public ShotPool(TextureRegion mBulletTextureRegion, FixtureDef fixtureDef) {
		if (mBulletTextureRegion == null) {
			throw new IllegalArgumentException("The texture region must not be NULL");
		}
		if (fixtureDef == null) {
			throw new IllegalArgumentException("The fixture definition must not be NULL");
		}
		mTextureRegion = mBulletTextureRegion;
		mFixtureDef = fixtureDef;
		mScene = Game.mScene;
		pw = Game.mPhysicsWorld;
		batchAllocatePoolItems(20);
	}

	public ShotPool(TextureRegion mBulletTextureRegion, FixtureDef fixtureDef, Scene scene, PhysicsWorld pw) {
		this.pw = pw;
		if (mBulletTextureRegion == null) {
			throw new IllegalArgumentException("The texture region must not be NULL");
		}
		if (fixtureDef == null) {
			throw new IllegalArgumentException("The fixture definition must not be NULL");
		}
		mTextureRegion = mBulletTextureRegion;
		mFixtureDef = fixtureDef;
		mScene = scene;
		batchAllocatePoolItems(20);
	}

	/**
	 * Called when a Bullet is required but there isn't one in the pool
	 */
	@Override
	protected Shot onAllocatePoolItem() {
		Debug.d("ShotPool", "onAllocatePoolItem");
		// Shot s = new Shot(mCannon.getShotOrigin(false),
		// mTextureRegion.deepCopy(), mFixtureDef);
		Shot shot = new Shot(Vector2Pool.obtain(0, 0), mTextureRegion.deepCopy(), mFixtureDef);
		 shot.deactivate();
		// GameController.mPhysicsWorld.registerPhysicsConnector(new
		// PhysicsConnector(shot.sprite, shot.body, true, true));
		// GameController.mScene.attachChild(shot.sprite);
		pw.registerPhysicsConnector(new PhysicsConnector(shot.sprite, shot.body, true, true));
		mScene.attachChild(shot.sprite);
		return shot;
	}

	/**
	 * Called when a Bullet is sent to the pool
	 */
	@Override
	protected void onHandleRecycleItem(final Shot pBullet) {
		Debug.d("ShotPool", "onHandleRecycleItem");
		pBullet.deactivate();
	}

	/**
	 * Called just before a Bullet is returned to the caller, this is where you
	 * write your initialize code i.e. set location, rotation, etc.
	 */
	@Override
	protected void onHandleObtainItem(final Shot pBullet) {
		Debug.d("ShotPool", "onHandleObtainItem");
		// final Vector2 temp =
		// Vector2Pool.obtain(mCannon.getShotOrigin(false)).mul(1f / 32f);
		// pBullet.body.setTransform(temp.x, temp.y, 0);
		// Vector2Pool.recycle(temp);
		// if
		// (!GameController.getInstance().mScene.equals(pBullet.sprite.getParent()))
		// {
		//
		// }
//		pBullet.activate();
	}
}
